package com.cheejee.goodscenter.module.dto;

import static com.cheejee.goodscenter.util.StaticField.GOODS_PIC_SPLIT;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import com.cheejee.goodscenter.module.entity.Goods;
import com.cheejee.goodscenter.module.entity.GoodsBrandProperties;
import com.cheejee.goodscenter.module.entity.GoodsCategoryProperties;
import com.cheejee.goodscenter.module.entity.GoodsCustomProperties;
import com.cheejee.goodscenter.module.entity.GoodsSpecific;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.cheejee.goodscenter.validated.UpdateGroup;
import com.fasterxml.jackson.annotation.JsonFormat;

import cn.hutool.core.collection.CollectionUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author CARRY
 * @Date 2020年10月30日下午5:07:15
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class GoodsDto{
	
	@NotNull(message = "商品ID不能为空", groups = UpdateGroup.class)
	private Long id;

	@NotNull(message = "商品品牌不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	private Integer brandId;

	@NotNull(message = "商品分类不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	private Integer categoryId;

	@NotNull(message = "商品名称不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	private String goodsName;

	@Length(max = 200, message = "商品副标题长度不能超过200个字", groups = {InsertGroup.class, UpdateGroup.class})
	private String subtitle;

	@NotEmpty(message = "商品图片不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	@Size(max = 8, message = "商品图片不能超过8张", groups = {InsertGroup.class, UpdateGroup.class})
	private List<String> pics;
	
	@NotNull(message = "商品单位不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	@Length(max = 30200, message = "商品单位长度不能超过200个字", groups = {InsertGroup.class, UpdateGroup.class})
	private String unit;

	@NotNull(message = "商品上架状态不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	private Boolean isOn;

	@NotNull(message = "商品删除状态不能为空", groups = {InsertGroup.class, UpdateGroup.class})
	private Boolean isDetelete;
	
	private Integer sale;

	private String webDetail;

	private String mobileDetail;
	
	private List<GoodsSpecific> specific;
	
	private List<SkuDto> sku;
	
	private Date createTime;

	private Date updateTime;
	
	private List<GoodsBrandProperties> brandProperties;
	
	private List<GoodsCategoryProperties> categoryProperties;
	
	private List<GoodsCustomProperties> customProperties;
	
	public Goods toGoods() {
		String p = null;
		if(!CollectionUtil.isEmpty(pics)) {
			StringBuilder sb = new StringBuilder();
			pics.forEach(i -> sb.append(i + GOODS_PIC_SPLIT));
			p = sb.substring(0, sb.length() - 1);
		}
		Goods g = new Goods();
		g.setId(id);
		g.setBrandId(brandId);
		g.setCategoryId(categoryId);
		g.setGoodsName(goodsName);
		g.setSubtitle(subtitle);
		g.setPics(p);
		g.setPublicityPic(pics.get(0));
		g.setUnit(unit);
		g.setIsOn(isOn);
		g.setIsDetelete(isDetelete);
		g.setWebDetail(webDetail);
		g.setMobileDetail(mobileDetail);
		g.setUpdateTime(null);
		g.setCreateTime(null);
		return g;
	}
	
}
